﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace YSGetLatest.DataObjects
{
	/// <summary>
	/// Holds data about folders.
	/// </summary>
	[Serializable]
	public class Project : BaseDataObject
	{
		#region Fields

		private string name_;
		private string nameSaved_;

		private string sourceLocation_;
		private string sourceLocationSaved_;
		
		private string branch_;
		private string branchSaved_;

		private ObservableCollection<Folder> folders_;
		private ObservableCollection<Folder> foldersSaved_;

		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the project name.
		/// </summary>
		public string Name
		{
			get
			{
				return name_;
			}
			set
			{
				name_ = value;
				SetPropertyChanged("Name");
			}
		}

		/// <summary>
		/// Gets or sets the source location.
		/// </summary>
		/// <value>
		/// The SourceLocation specifies the TFS source location, e.g. $/Company/Development/ProjectName 
		/// </value>
		public string SourceLocation
		{
			get
			{
				return sourceLocation_;
			}
			set
			{
				sourceLocation_ = value;
				SetPropertyChanged("SourceLocation");
			}
		}

		/// <summary>
		/// Gets or sets the source location.
		/// </summary>
		/// <value>
		/// The SourceLocation specifies the TFS source location, e.g. $/Company/Development/ProjectName 
		/// </value>
		public string Branch
		{
			get
			{
				return branch_;
			}
			set
			{
				branch_ = value;
				SetPropertyChanged("Branch");
			}
		}

		/// <summary>
		/// Gets the list of the folders.
		/// </summary>
		public ObservableCollection<Folder> Folders
		{
			get
			{
				if (folders_ == null)
				{
					folders_ = new ObservableCollection<Folder>();
				}
				return folders_;
			}
		}

		/// <summary>
		/// Gets or sets a value that indicates whether a Project is selected.
		/// </summary>
		/// <value>
		/// true if the item is selected; otherwise, false. The default is false.
		/// </value>
		[XmlIgnore]
		public bool IsSelected
		{
			get;
			set;
		}
		#endregion

		#region BaseDataObject Methods

		/// <summary>
		/// Saves data members.
		/// </summary>
		protected override void Save()
		{
			nameSaved_ = Name;
			sourceLocationSaved_ = SourceLocation;
			branchSaved_ = Branch;

			foldersSaved_ = new ObservableCollection<Folder>(Folders.ToList());

			// Save old values for each project property.
			foreach (var folder in foldersSaved_)
			{
				folder.BeginEdit();
			}
		}

		/// <summary>
		/// Restores data members.
		/// </summary>
		protected override void Restore()
		{
			Name = nameSaved_;
			SourceLocation = sourceLocationSaved_;
			Branch = branchSaved_;

			// Restore old values for each project property.
			foreach (var folder in foldersSaved_)
			{
				folder.CancelEdit();
			}

			folders_ = new ObservableCollection<Folder>(foldersSaved_.ToList());

			SetPropertyChanged("Folders");
		}
		#endregion

		#region Methods

		/// <summary>
		/// Returns the text representation of the instance.
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			var buffer = new StringBuilder();

			buffer.AppendFormat("{0}:", GetType().Name);
			buffer.AppendFormat(" Name: \"{0}\"", Name);
			buffer.AppendFormat(", SourceLocation: \"{0}\"", SourceLocation);
			buffer.AppendFormat(", Branch: \"{0}\"", Branch);
			buffer.Append(", Folders:[");

			bool firstItem = true;
			foreach (var folder in Folders)
			{
				if (!firstItem)
				{
					buffer.Append(", ");
				}
				else
				{
					firstItem = false;
				}
				buffer.Append(folder.ToString());
			}
			buffer.Append("]");

			return buffer.ToString();
		}
		#endregion
	}
}
